package com.leetcode;

/**
 * 905. 按奇偶排序数组
 * 双指针原地交换 奇偶元素
 *
 * 2022-04-28 每日一题
 *
 * @author fy
 * @date 2022/4/28 9:05
 */
public class Solution905 {

    public int[] sortArrayByParity(int[] nums) {
        if (nums == null || nums.length == 1) {
            return nums;
        }
        int n = nums.length;
        int l = 0;
        int r = n - 1;
        while (l < r) {
            if (nums[r] % 2 == 0) {
                int tmp = nums[r];
                nums[r] = nums[l];
                nums[l] = tmp;
                l++;
            } else {
                r--;
            }
        }
        return nums;
    }

}
